import { GetanDrawerProps, DrawerMethods, UseDrawerReturnType } from "./types"

export function useGetanDrawer(props: GetanDrawerProps): UseDrawerReturnType {
    const modalRef = ref<Nullable<DrawerMethods>>(null)

    const getInstance = () => {
        const instance = unref(modalRef.value)
        if (!instance) {
            console.error("useModal instance is undefined!")
        }
        return instance
    }

    const register = (modalInstance: DrawerMethods) => {
        modalRef.value = modalInstance
        modalInstance.init && modalInstance.init(props)
    }

    const methods: DrawerMethods = {
        open: title => getInstance()?.open(title),
        close: () => getInstance()?.close()
    }

    return { register, methods }
}
